package com.xiexuan.hearthstonecard;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import android.view.animation.Animation.AnimationListener;
import android.widget.Button;

import android.widget.ImageView;

import android.widget.TextView;

public class Welcome_activity extends Activity {

	private ArrayList<View> listview = new ArrayList<View>();
	private ImageView iv_leader1;
	private ImageView iv_leader2;
	private TextView tv_leader1;
	private Button btn_leader2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);

		ViewPager viewpager = (ViewPager) this
				.findViewById(R.id.intoduce_viewpager);

		LayoutInflater inflater = LayoutInflater.from(this);

		View tab01 = inflater.inflate(R.layout.leader1_fragment, null);
		View tab02 = inflater.inflate(R.layout.leader2_fragment, null);

		iv_leader1 = (ImageView) tab01.findViewById(R.id.iv_leader1);
		iv_leader2 = (ImageView) tab02.findViewById(R.id.iv_leader2);
		tv_leader1 = (TextView) tab01.findViewById(R.id.tv_leader1);
		btn_leader2 = (Button) tab02.findViewById(R.id.btn_leader2);

		btn_leader2.setVisibility(View.INVISIBLE);

		final Animation animation = AnimationUtils.loadAnimation(this,
				R.anim.scale_rotate_anim);

		final Animation animation1 = AnimationUtils.loadAnimation(this,
				R.anim.scale);
		final Animation animation2 = AnimationUtils.loadAnimation(this,
				R.anim.animplus);
		final Animation animation3 = AnimationUtils.loadAnimation(this,
				R.anim.scale2);

		iv_leader1.startAnimation(animation);

		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				tv_leader1.setVisibility(View.INVISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				tv_leader1.setVisibility(View.VISIBLE);
				tv_leader1.startAnimation(animation1);
			}
		});

		listview.add(tab01);
		listview.add(tab02);

		viewpager.setAdapter(new PagerAdapter() {
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				// TODO Auto-generated method stub
				return arg0 == arg1;
			}

			@Override
			public Object instantiateItem(ViewGroup container, int position) {
				// TODO Auto-generated method stub
				container.addView(listview.get(position));
				return listview.get(position);
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				// TODO Auto-generated method stub
				// super.destroyItem(container, position, object);
				container.removeView(listview.get(position));
			}

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return listview.size();
			}
		});

		viewpager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				switch (arg0) {
				case 0:

					iv_leader1.startAnimation(animation);
					animation.setAnimationListener(new AnimationListener() {

						@Override
						public void onAnimationStart(Animation animation) {
							// TODO Auto-generated method stub
							tv_leader1.setVisibility(View.INVISIBLE);
						}

						@Override
						public void onAnimationRepeat(Animation animation) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onAnimationEnd(Animation animation) {
							// TODO Auto-generated method stub
							tv_leader1.setVisibility(View.VISIBLE);
							tv_leader1.startAnimation(animation1);
						}
					});

					break;
				case 1:

					btn_leader2.setVisibility(View.INVISIBLE);
					iv_leader2.startAnimation(animation2);
					iv_leader2.setVisibility(View.INVISIBLE);
					animation2.setAnimationListener(new AnimationListener() {

						@Override
						public void onAnimationStart(Animation animation) {
							// TODO Auto-generated method stub
							btn_leader2.setVisibility(View.INVISIBLE);
						}

						@Override
						public void onAnimationRepeat(Animation animation) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onAnimationEnd(Animation animation) {
							// TODO Auto-generated method stub
							btn_leader2.setVisibility(View.VISIBLE);
							btn_leader2.startAnimation(animation3);

						}
					});
					break;

				default:
					break;
				}
			}
		});
	}

	public void Start_Main(View view) {
		Intent mIntent = new Intent(this, MainActivity.class);
		startActivity(mIntent);
	}

}
